package com.guigu.gulicommon.entity.pojo.res;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.gulicommon.api.BaseResCode;
import com.guigu.gulicommon.constant.enumes.res.CommonResCode;
import com.guigu.gulicommon.constant.enumes.res.ServerResCode;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.util.Collections;
import java.util.List;

/**
 * 通用返回结果
 *
 * @param <T>
 * @author 陈俊杰
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class Res<T> {

    @ApiModelProperty(value = "状态码")
    private Integer code;
    @ApiModelProperty(value = "返回信息")
    private String msg;
    @ApiModelProperty(value = "总记录数")
    private Long total;
    @ApiModelProperty(value = "当前页记录数")
    private Long records;
    @ApiModelProperty(value = "页码")
    private Long pageNum;
    @ApiModelProperty(value = "分页大小")
    private Long pageSize;
    @ApiModelProperty(value = "数据")
    private List<T> data;


    /**
     * 数据为空的响应体
     *
     * @param resCode 响应码
     */
    public Res(BaseResCode resCode) {
        this.code = resCode.getCode();
        this.msg = resCode.getMsg();
        this.total = 0L;
        this.pageNum = null;
        this.pageSize = null;
        this.data = null;
        this.records = 0L;
    }

    /**
     * 用于mybatisplus分页
     *
     * @param resCode 响应码枚举类
     * @param page    分页数据
     */
    public Res(BaseResCode resCode, Page<T> page) {
        this.code = resCode.getCode();
        this.msg = resCode.getMsg();
        this.total = page.getTotal();
        this.pageNum = page.getCurrent();
        this.pageSize = page.getSize();
        this.data = page.getRecords();
        this.records = (long) page.getRecords().size();
    }


    /**
     * 用于自定义分页
     *
     * @param resCode 响应码枚举类
     * @param page    分页数据
     */
    public Res(BaseResCode resCode, PagePoJo<T> page) {
        this.code = resCode.getCode();
        this.msg = resCode.getMsg();
        this.total = page.getTotal();
        this.pageNum = page.getPageNum();
        this.pageSize = page.getPageSize();
        this.data = page.getData();
        this.records = page.getRecords();
    }

    public Res(BaseResCode resCode, List<T> data) {
        this.code = resCode.getCode();
        this.msg = resCode.getMsg();
        if (data == null || data.isEmpty()) {
            this.data = null;
            this.total = 0L;
            this.records = 0L;
        } else {
            this.data = data;
            this.total = (long) this.data.size();
            this.records = (long) this.data.size();
        }
    }

    public Res(BaseResCode resCode, T data) {
        this.code = resCode.getCode();
        this.msg = resCode.getMsg();
        if (data == null) {
            this.data = null;
            this.total = 0L;
            this.records = 0L;
        } else {
            this.data = Collections.singletonList(data);
            this.total = (long) this.data.size();
            this.records = (long) this.data.size();
        }
    }

    /**
     * @param resCode 响应码
     * @param msg     提示消息
     */
    public Res(Integer resCode, String msg) {
        this.code = resCode;
        this.msg = msg;
        this.total = 0L;
        this.pageNum = null;
        this.pageSize = null;
        this.data = null;
        this.records = 0L;
    }

    /**
     * 默认成功返回值
     */
    public static <T> Res<T> success() {
        return new Res<>(CommonResCode.SUCCESS);
    }

    /**
     * 默认失败返回值
     */

    public static <T> Res<T> error() {
        return new Res<>(ServerResCode.UNKNOWN_EXCEPTION);
    }

    /**
     * 转Json格式
     */
    public String toJson() {
        return "{\"code\":" + this.code + ",\"msg\":\""
                + this.msg + "\",\"total\":" + this.total +
                ",\"pageNum\":" + this.pageNum + ",\"pageSize\":"
                + this.pageSize + ",\"data\":" + this.data + "}";
    }


}
